#include "timer.h"

hw_timer_t* timer;
bool lcd_open;

void tim1Interrupt()
{
    static int i=0;
    if(lcd_open)
    {
        i++;
        ledcWrite(0,i);
        if(i==255)
            timerAlarmDisable(timer);
    }
    else{
        i--;
        ledcWrite(0,i);
        if(i==0)
            timerAlarmDisable(timer);
    }
}

void LCD_PWM_init()
{
    pinMode(2,OUTPUT);
    digitalWrite(2,0);
    ledcSetup(0,2000,8);
    ledcAttachPin(2,0);
    // for(int i=0;i<255;i++)
    // {
    //     ledcWrite(0,i);
    //     delay(10);
    // }
    timer = timerBegin(1,80,true);
    timerAttachInterrupt(timer,tim1Interrupt,true);
    timerAlarmWrite(timer,10000,true);
}

void LCD_Display_Set(bool en)
{
    lcd_open = en;
    if(en)
    {
        timerAlarmEnable(timer);
    }
    else{

    }
}
